package com.example.zz.example.anr;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.zz.example.R;

import java.io.File;
import java.io.FileOutputStream;

/**
 *
 * 老版本手机
 * adb pull /data/anr/traces.txt .
 * 新版本手机
 * adb bugreport .
 *
 */

public class AnrActivity extends AppCompatActivity {

    private boolean isStartIo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_anr);

    }

    public void activityOtherEvent(View view) {
        Toast.makeText(this, "点击弹toast", Toast.LENGTH_SHORT).show();
    }

    public void activityStartIo(View view) {
        try {
            //data/data/包名/file
            File filesDir = AnrActivity.this.getFilesDir();
            String logPath = filesDir.getAbsolutePath();
            File dir = new File(logPath);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            FileOutputStream fileOutputStream = new FileOutputStream(logPath + "/" + "aaa.txt");
            isStartIo = true;
            Thread.sleep(30_000);
//            while (isStartIo) {
//                byte[] byteBuf = new byte[1024];
//                for (int i = 0; i < byteBuf.length; i++) {
//                    String chars = "ABCDEFGHIZKLMNOPQRSTUVWXYZ";
//                    char c = chars.charAt((int) (Math.random() * 26));
//                    byteBuf[i] = ((byte) c);
//                }
//                fileOutputStream.write(byteBuf);
//            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void activityStopIo(View view) {
        isStartIo = false;
    }
}